package cn.tedu.micro.demo.rabbit.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class DeclareConfiguration {
    public static final String TRANS_EX="trans_ex";
    public static final String TRANS_Q="trans_q";
    public static final String DELAY_EX="delay_ex";
    public static final String DELAY_Q="delay_q";
    public static final String DELAY_RK="delay_rk";
    //为上述的命名 声明一套延迟队列的结构
    //1.业务交换机
    @Bean
    public Exchange transEx(){
        //channel.exchangeDeclare("",DIRECT)
        //1.直接new
        return ExchangeBuilder.fanoutExchange(TRANS_EX).build();
    }
    //2.延迟交换机
    @Bean
    public Exchange delayEx(){
        return ExchangeBuilder.directExchange(DELAY_EX).build();
    }
    //3.延迟队列
    @Bean
    public Queue delayQ(){
        return QueueBuilder.nonDurable(DELAY_Q).build();
    }
    //4.业务队列
    @Bean
    public Queue transQ(){
        return QueueBuilder.nonDurable(TRANS_Q).deadLetterRoutingKey(DELAY_RK).deadLetterExchange(DELAY_EX).build();
    }
    //两组绑定关系
    @Bean
    public Binding bindTrans(){
        return BindingBuilder.bind(transQ()).to(transEx()).with("").noargs();
    }
    @Bean
    public Binding bindDelay(){
        return BindingBuilder.bind(delayQ()).to(delayEx()).with(DELAY_RK).noargs();
    }
    //queue01
    @Bean
    public Queue queue01(){
        //channel.queueDeclare("",false,false,false,null)
        //1.直接new
        //new Queue("queue01",false,false,false,null);
        //2.使用builder 直观灵活
        //QueueBuilder.nonDurable("queue01").exclusive().autoDelete().deadLetterExchange("").build();
        return QueueBuilder.nonDurable("queue01").build();
    }
    //ex01 fanout
    @Bean
    public Exchange ex01(){
        //channel.exchangeDeclare("",FANOUT)
        //1.直接new
        return ExchangeBuilder.fanoutExchange("ex01").build();
    }
    //bind ""
    @Bean
    public Binding bind01(){
        //channel.queueBind("","","")
        //1.直接new
        //2.BindingBuilder构建 routingKey header
        return BindingBuilder.bind(queue01()).to(ex01()).with("").noargs();
    }
}
